Overview
This guide walks you through three practical examples demonstrating common tasks with GDAL Scripts. Each example uses real scripts from the repository with actual command signatures.These examples assume you have completed the installation steps and have test planetary imagery available. You can download sample data from USGS Astrogeology Data.
Example 1: Coordinate Conversion
One of the most common tasks in planetary data processing is converting between different coordinate systems. Thegdal2Coordinates directory provides four utilities for this purpose.
Converting Pixel Coordinates to Latitude/Longitude
Thepixel2longlat.py script reads the coordinate system and geotransformation matrix from an image and reports latitude/longitude coordinates for a specified pixel.
Understand the Script
Script location: Parameters:
gdal2Coordinates/pixel2longlat.pyPurpose: Given pixel (sample) and line coordinates, report the latitude/longitude coordinates for the center of that pixel.Usage:sample: Pixel column number (x-coordinate)line: Pixel row number (y-coordinate)infile: Input geospatial image file
Run the Example
Find the coordinates of pixel (100, 200) in a lunar image:Expected Output:The script outputs coordinates in both decimal degrees and DMS (degrees, minutes, seconds) format.
Understanding the Output
The coordinate conversion accounts for:- The image’s geotransformation matrix (position, rotation, scale)
- The coordinate reference system (CRS) defined in the image
- Pixel center vs. pixel corner conventions
The script automatically shifts to the center of the pixel by adding half the pixel size to the calculated coordinates.
Example 2: Terrain Analysis with Slope Calculation
Thegdal_baseline_slope.py script calculates specialized slopes using various baseline lengths, a method developed specifically for planetary terrain analysis.
Understand Baseline Slopes
Script location:
gdal_baseline_slope/gdal_baseline_slope.pyPurpose: Calculate slopes using variable baseline lengths measured in pixels. Different baselines reveal terrain characteristics at different scales.Key Concepts:- Baseline = 1, 2, or 5: Uses corner pixels of a moving window
- No baseline specified: Uses Horn’s Method (3×3 window, equivalent to
gdaldem slope)
Calculate Standard Slope (Horn's Method)
Without specifying a baseline, the script uses Horn’s Method:Output:This produces a 32-bit floating-point slope raster in degrees.
Calculate Baseline Slope
For specialized terrain analysis with a 5-pixel baseline:The baseline parameter changes which pixels are used in the calculation:
- Baseline 2: Uses 3×3 window corners
- Baseline 5: Uses 6×6 window corners
Generate 8-bit Output
For visualization or to reduce file size, create 8-bit output:This scales slope values (0-50+ degrees) to 1-255 using:The script generates both 32-bit and 8-bit versions:
32bit_mars_slope_8bit.tif(full precision)mars_slope_8bit.tif(scaled to byte)
Performance Considerations
Dependencies
This script requires:- NumPy: Array operations
- SciPy: Generic filtering functions
Example 3: Clipping Data to Valid Ranges
Thegdal_clip2range.py script is essential for cleaning planetary data by setting out-of-range values to NoData.
Understand the Use Case
Script location:
gdal_clip2range/gdal_clip2range.pyPurpose: Clip pixel values to a defined valid range. Values outside this range are set to NoData.Common applications:- Removing erroneous values from I/F (reflectance) data
- Eliminating elevation outliers from DEMs
- Cleaning up processed imagery
Clip with Existing NoData Value
Most planetary images have a NoData value defined. Preserve it:What this does:
- Sets pixels < -100.5 meters to NoData
- Sets pixels > 500.2 meters to NoData
- Maintains the original NoData value from the input file
- Preserves all pixels within the valid range
Set Valid Range for Reflectance Data
For I/F (reflectance) images that should only contain values between 0 and 1:Note the fifth parameter: Here we explicitly set the output NoData value to 0 because:
- The input file may not have NoData defined
- For reflectance data, 0 is a reasonable NoData value
- All invalid pixels (< 0 or > 1) will be set to 0
Technical Details
The script:- Opens the input file and creates a copy for output
- Reads each band as a NumPy array
- Applies the range mask:
data[(data < minValue) | (data > maxValue)] = noData - Writes the modified array back to the output file
- Clears existing statistics metadata (which would be invalid)
For more information and community discussion, see: OpenPlanetary: Crop Image DN Range
Next Steps
Explore More Tools
Now that you’ve tried these examples, explore other categories:Format Conversion
Convert to ISIS3, PDS4, or generate point clouds:
gdal2ISIS3/Astropedia_gdal2ISIS3.pyPDS4gdal/isis3_to_pds4_LOLA_pvl.pygdal2PLY/gdal2PLY.py
Projection Tools
Work with IAU coordinate reference systems:
OGC_IAU2000_WKT_v2/Source_Python/create_IAU2000_wkt_v3.pyNewCenterLon_Equi/NewCenterLon_Equi.py
Metadata Operations
Extract and manipulate image metadata:
gdal2metadata/gdal2metadata.pygdal_copylabel/gdal_copylabel.py
Spatial Data
Work with vectors and spatial features:
ogr_footprintinit2shp/footprintinit2shp.pyogr_isisminer2shp/isisminer2shp.py
Batch Processing
For processing multiple files, create shell scripts or Python wrappers:Example Batch Script
Getting Help
Each script directory contains a README.md with detailed usage instructions:Common Workflows
Landing Site Analysis Workflow
Landing Site Analysis Workflow
- Download DEM of the region of interest
- Clip to valid range using
gdal_clip2range.py - Calculate slopes at multiple baselines:
- Extract coordinates of potential sites using
pixel2longlat.py - Export to shapefile for analysis in GIS software
Image Mosaic Preparation
Image Mosaic Preparation
- Match image extents using
gdal_match_image_extents.py - Clip to valid ranges for consistent data quality
- Convert projection if needed using IAU WKT definitions
- Mosaic using standard GDAL tools like
gdal_merge.py
Data Archive Conversion
Data Archive Conversion
- Convert from ISIS3 cubes to GeoTIFF for web services
- Generate PDS4 labels using
PDS4gdalscripts for archiving - Create metadata extracts using
gdal2metadata.py - Generate footprint shapefiles for inventory database
Report Issues or Contribute
Found a bug or have a feature request? Visit the GitHub repository to open an issue or contribute improvements.